cc.Class({
    extends: cc.Component,

    properties: {
        winLayout: cc.Layout,
        lostLayout: cc.Layout,
        playerInfoItem:cc.Node,  
        progressBar:cc.Node,
        btn:{
            default: [],
            type: cc.Node
        }

    },

    // use this for initialization
    onLoad: function () {
        this.node.scaleX = 0.1;
        this.node.scaleY = 0.1;
        let scaleTo = cc.scaleTo(0.85,1.0);
        this.node.runAction(scaleTo.easing(cc.easeElasticOut(0.85)));
        // this.registerEvent()
    },

    registerEvent:function(){
        // 注册事件
        window.sock.node.on("11-4", (event)=>{     // 
            // console.log("event data:",  event)
        });

        // 监听游戏结束，删除所有玩家

    },

    start:function(){
        this.playerInfoItem.removeFromParent();
        this.updateInfo([]);
        this.registerTimer(5);

        for(let i =0; i < this.btn.length; ++i){
            this.btn[i].on('click', this.onClickButton, this);
            this.btn[i].tag = i;
        }    
    },

    updateInfo:function(data){
        let selfIndex = Math.floor(Math.random()*5); 
        for(let i =0; i<4; ++i){
           let dataItem = {name:"helloworld", isWin:(Math.random()>0.5), score:1000, isHost:(i==0)};
           data.push(dataItem);
        }
        // console.log(data)

        for(let i =0; i<data.length; ++i){     
            // 拷贝节点
            let playerNode = cc.instantiate(this.playerInfoItem);
            //设置添加的父节点 
            if( data[i].isWin == true){
                playerNode.parent = this.winLayout.node
            }else{
                playerNode.parent = this.lostLayout.node
            }
            playerNode.getComponent("TxResultPlayerInfo").playerName.string = data[i].name
            playerNode.getComponent("TxResultPlayerInfo").playerScore.string = data[i].score
            playerNode.getComponent("TxResultPlayerInfo").playerHostIcon.node.active = false 
            // playerNode.getComponent("TxResultPlayerInfo").playerIcon                     //更换头像
        }
        
    },

    onClickButton:function(event){
        console.log(event.target)
        if(event.target.tag == 0){             // 不玩了

        }else if(event.target.tag == 1 ){      // 继续玩

        }else if(event.target.tag == 2  ){     // 知道了 
            
        }
        this.node.active =false
    },

    registerTimer:function(time){
        //倒计时
        time = time *1000
        var intervale = (1/60)*1000  //毫秒
        var cur_time = 0
        var progressBar = this.progressBar.getComponent(cc.ProgressBar)
        var timer_id = setInterval(()=>{
            cur_time += intervale
            progressBar.progress = (1- cur_time/time)
            if(progressBar.progress <= 0 ){
               progressBar.progress = 0
               clearInterval(timer_id);
            }
        },intervale)
    },

});
